package com.infovane.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.infovane.dao.BaseDao;

public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{
	
	public static Logger logger=Logger.getLogger(BaseDaoImpl.class);
	
	/**
	 * 加载一个对象
	 * @param modelClass id
	 * @return Object
	 * */
	public Object load(Class modelClass, Long id) {
		Object object=null;
		try{
			object=this.getHibernateTemplate().load(modelClass, id);
			logger.info("load successful!");
			return object;
		}catch(DataAccessException e){
			logger.info("load failed!",e);
		}
		return object;
		
	}
	
	/**
	 * 分页加载数据
	 * @param hql,first,count
	 * @return List
	 * */
	public List load(final String hql,final int first,final int count) {
		List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery(hql);
				query.setFirstResult(first);
				query.setMaxResults(count);
				List list=query.list();
				logger.info("load successful!");
				return list;
			}
			
		});
		return list;
	}
	
	/**
	 * 加载数据
	 * @param hql
	 * @return List
	 * */
	public List load(String hql) {
		List list=null;
		try{
			list=this.getHibernateTemplate().find(hql);
			logger.info("load successful!");
			return list;
		}catch(DataAccessException e){
			logger.info("load failed!",e);
		}
		return list;
	}
	
	/**
	 * 保存一个对象
	 * @param object
	 * @return boolean
	 * */
	public boolean save(Object object){
		try{
			this.getHibernateTemplate().save(object);
			logger.info("save successful!");
			return true;
		}catch(DataAccessException e){
			logger.info("save failed!",e);
		}
		return false;
	}
	
	/**
	 * 删除一个对象
	 * @param modelClass,id
	 * @return boolean;
	 * */
	public boolean delete(Class modelClass,int id) {
		try{
			Object object=this.getHibernateTemplate().load(modelClass, id);
			this.getHibernateTemplate().delete(object);
			logger.info("delete successful!");
			return true;
		}catch(DataAccessException e){
			logger.info("delete failed!",e);
		}
		return false;
	}

	/**
	 * 删除一个对象
	 * @param modelClass,id
	 * @return boolean;
	 * */
	public boolean delete(Class modelClass,Long id) {
		try{
			Object object=this.getHibernateTemplate().load(modelClass, id);
			this.getHibernateTemplate().delete(object);
			logger.info("delete successful!");
			return true;
			
		}catch(DataAccessException e){
			logger.info("delete failed!",e);
		}
		return false;
	}
	
	/**
	 * 获取一个对象
	 * @param modelClass,id
	 * @return Object
	 * */
	public Object load(Class modelClass, int id) {
		Object object=null;
		try{
			object=this.getHibernateTemplate().load(modelClass, id);
			logger.info("load successful！");
			return object;
		}catch(DataAccessException e){
			logger.info("load failed!",e);
		}
		return object;
	}
	
	/**
	 * 更新（修改）一个对象
	 * @param Object
	 * @return boolean
	 * */
	public boolean update(Object object) {
		try{
			this.getHibernateTemplate().update(object);
			logger.info("update successful!");
			return true;
		}catch(DataAccessException e){
			logger.info("update failed!",e);
		}
		return false;
	}
}
